import Vue from 'vue'
import axios from 'axios'
import router from '@/router'
import qs from 'qs'
import merge from 'lodash/merge'


// axios.defaults.withCredentials = true;

const http = axios.create({
    timeout: 1000 * 300,
    // withCredentials: true,
    headers: {
        // 'Content-Type': 'application/json; charset=utf-8',
        "Content-Type": "application/x-www-form-urlencoded"
    },
    transformRequest: [
        function(data) {
            //return JSON.stringify(data);
            let ret = "";
            for (let it in data) {
                ret += it + "=" + encodeURIComponent(data[it]) + "&";
            }
            return ret;
        }
    ]
})

/**
 * 请求拦截
 */
http.interceptors.request.use(config => {
    return config
}, error => {
    return Promise.reject(error)
})

/**
 * 响应拦截
 */
http.interceptors.response.use(response => {
    return response
}, error => {
    return Promise.reject(error)
})

/**
 * 请求地址处理
 * @param {*} actionName action方法名称
 */
http.adornUrl = (actionName, who = '') => {
    if (who) {
        let devUrl, proUrl
        switch (who) {
            case 'weixin':
                return ('/weixin' + actionName)
            case 'proxyHty':
                return ('https://sz.yewen1001.com' + '/server/mobile/v1/public/index.php' + actionName)
            case 'proxyHtyLocal':
                devUrl = '/proxyHtyLocal' + '/server/yezhu/v1/public/index.php' + actionName; // 线上
                
                proUrl = process.env.YEWEN_URL + '/server/yezhu/v1/public/index.php' + actionName; // 不同环境的请求地址
                return process.env.NODE_ENV === 'production' ? proUrl : devUrl;
                //http://192.168.60.172/m5.yl1001.com/server/controller/v1/public/index.php/luckdraw/index/getLuckdrawSubject?cj_id=5081614069786353
            case "proxyPhp":
            case "proxyHL":
                devUrl = '/proxyPhp' + '/controller/v1/public/index.php/' + actionName;
                proUrl = '' + '/controller/v1/public/index.php/' + actionName;
                return process.env.NODE_ENV === 'production' ? proUrl : devUrl;
                // return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? who : '//m5.yl1001.com') + '/controller/v1/public/index.php/' + actionName//https://live.gzrc.com.cn/webservice/livewebservice.php
            // case "proxyHL":
            //     devUrl = '/proxyPhp' + '/controller/v1/public/index.php/' + actionName;
            //     proUrl = '' + '/controller/v1/public/index.php/' + actionName;
            //     return process.env.NODE_ENV === 'production' ? proUrl : devUrl;
            case "proxyXlh":
                devUrl = '/proxyXlh' + '/controller/v1/public/index.php/' + actionName;
                proUrl = '' + '/controller/v1/public/index.php/' + actionName;
                return process.env.NODE_ENV === 'production' ? proUrl : devUrl;
            default:
                break
        }
    }

    let url = '/webservice/live_video/index.jsp/v1'
        // 非生产环境 && 开启代理, 接口前缀统一使用[/proxyApi/]前缀做代理拦截!
    // return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi' : '//zhibo.yl1001.com') + url + actionName + "?token=dXNlciUzRGxpdmVfdmlkZW8lMjZwd2QlM0RsaXZlX3ZpZGVvX2RhdGFfam9iMTAwMQ=="
    return (process.env.NODE_ENV !== 'production' && process.env.OPEN_PROXY ? '/proxyApi' : process.env.ZHIBO_URL) + url + actionName + "?token=dXNlciUzRGxpdmVfdmlkZW8lMjZwd2QlM0RsaXZlX3ZpZGVvX2RhdGFfam9iMTAwMQ=="

}

/**
 * get请求参数处理
 * @param {*} params 参数对象
 * @param {*} openDefultParams 是否开启默认参数?
 */
http.adornParams = (params = {}, openDefultParams = true) => {
    var defaults = {
        't': new Date().getTime()
    }
    return openDefultParams ? merge(defaults, params) : params
}

/**
 * post请求数据处理
 * @param {*} data 数据对象
 * @param {*} openDefultdata 是否开启默认数据?
 * @param {*} contentType 数据格式
 *  json: 'application/json; charset=utf-8'
 *  form: 'application/x-www-form-urlencoded; charset=utf-8'
 */
http.adornData = (data = {}, openDefultdata = true, contentType = 'json') => {
    var defaults = {
        't': new Date().getTime()
    }
    data = openDefultdata ? merge(defaults, data) : data
    return contentType === 'json' ? JSON.stringify(data) : qs.stringify(data)
}


http.postForm = (params) => {
    // 写一个原生的吧，非原生的aixos请求会报跨域错误
    return new Promise((res, rej) => {
        var a = new FormData();
        //使用append方法向空的FormData对象添加字段
        for (let key in params.data) {
            console.log('key', key);
            a.append(key, params.data[key]); //上传文件  
        }
        var oReq = new XMLHttpRequest();
        oReq.onload = function(info) {
            if (info.target.status === 200) {
                // console.log('info', info);
                res(JSON.parse(info.target.response))
            }
        }
        oReq.open('POST', params.url);
        oReq.send(a)
    })
}
export default http